<template>
	<view class="container">
		<u-form :model="registerForm" ref="uForm">
		<u-form-item label="账号" label-width="160"><u-input v-model="registerForm.username" placeholder="请输入学号" type="text"/></u-form-item>
		<u-form-item label="密码" label-width="160"><u-input v-model="registerForm.password" type="password"/></u-form-item>
		<u-form-item label="确认密码" label-width="160"><u-input v-model="registerForm.confirmPassword" type="password" /></u-form-item>
		<u-form-item label="邮箱账号" label-width="160" ><u-input v-model="registerForm.email" type="text" placeholder="用于找回密码"/></u-form-item>
	</u-form>
	
	<view class="registerBtn">
		<u-button type="primary" :loading="regLoading" @click="onRegister">注册</u-button>
	</view>
	</view>
</template>

<script>
	import {register} from "@/api/register.js"
	var that;// 当前页面对象
	var vk; //vk依赖
	export default {
		data() {
			return {
				regLoading:false,
				registerForm:{
					username:'',
					password:'',
					confirmPassword:'',
					email:''
				}
			}
		},
		onLoad(options) {
			that = this;
			vk = that.vk
			that.options = options
			
		},
		methods: {
			onRegister(){
				if(this.registerForm.username&&this.registerForm.password&&this.registerForm.confirmPassword&&this.registerForm.email){
					if(this.registerForm.password == this.registerForm.confirmPassword){
						this.regLoading = true
						register(this.registerForm).then((data)=>{
							this.regLoading = false
							let [err,res] = data
							if(res.data.code == 200){
								vk.toast('注册成功,跳转登陆页面','success',800);
								vk.setVuex('$user.userInfo.username',this.registerForm.username)
								vk.setVuex('$user.userInfo.password',this.registerForm.password)
								setTimeout(()=>{
									vk.redirectTo('/pages/login/login');
								},800)
							}
							else {
								vk.alert(res.data.msg || err);
							}
						})
					}
					else {
						vk.toast('前后密码输入不一致，请检查','none');
					}
				}
				else {
					vk.toast('请完善注册信息','none');
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		padding: 10rpx 40rpx;
	}
</style>
